import {createRouter, createWebHashHistory} from 'vue-router'
import newsRoutes from './modules/news'
import projectModule from '@/router/modules/project'
import userModule from '@/router/modules/user'
import businessModule from '@/router/modules/business'
import Layout from '@/layout/index.vue'
import informationModule from "@/router/modules/information";
import store from '@/store'
import {toast} from "@/hook/toast";

const publicRoutes = [
    {
        path: 'profile',
        name: 'profile',
        meta: {title: '工作台', icon: 'user'},
        component: () => import('@/views/profile'),
    },
    {
        path: 'userinfo',
        name: 'userinfo',
        meta: {title: '个人中心', icon: ''},
        component: () => import('@/views/userinfo'),
    },
    {
        path: 'icon',
        name: 'iconPage',
        meta: {title: '图标', icon: ''},
        component: () => import('@/views/icons'),
    },
    // {
    //     path: 'news-upload',
    //     name: 'news-upload',
    //     meta: {title: '上传文件', icon: 'user'},
    //     component: () => import('@/views/news-upload'),
    // },
    {
        path: "chat",
        name: "chat",
        meta: {title: "员工任务看板", icon: "chart-histogram"},
        component: () => import("@/views/project-manage"),
    },
    {
        path: "progress",
        name: "progress",
        meta: {title: "项目进度看板", icon: "flame"},
        component: () => import("@/views/project-progress"),
    },
    {
        path: "message",
        name: "message",
        meta: {title: "消息", icon: ""},
        component: () => import("@/views/message"),
    },
    {
        path: "information/manage",
        name: 'informationManage',
        component: () => import('@/views/company-information'),
        meta: {title: '公司资料管理'},
    },
    {
        path: '404',
        name: '404',
        component: () => import('@/views/404/index')
    },
    {
        path: '403',
        name: '403',
        component: () => import('@/views/403/index')
    }
];


const routes = [
    {
        path: '/',
        name: 'index',
        redirect: {name: 'profile'},
        component: Layout,
        children: [
            ...publicRoutes,
        ],
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/login'),
    },
    // ...newsRoutes(Layout),
    projectModule(Layout),
    businessModule(Layout),
    informationModule(Layout),
    userModule(Layout),
    {
        path: '/:catchAll(.*)',
        redirect: '/404',
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

router.beforeEach((to, form, next) => {
    const whiteList = ['login', '404', '403']
    const {hasLogin} = store.getters
    if (to.name === 'login') {
        if (hasLogin) {
            return next(false)
        }
        return next()
    }
    if (!hasLogin) {
        toast({title: '登录后可进行更多操作', type: 'error'})
        return next({name: 'login'})
    }
    next()
})

export default router
